<?php

/**
 * This is the model class for table "account".
 *
 * The followings are the available columns in table 'account':
 * @property string $surname
 * @property string $title
 * @property integer $business_id
 * @property integer $account_id
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $tel
 * @property string $first_name
 * @property integer $role_id
 *
 * The followings are the available model relations:
 * @property Business $business
 */
class Account extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Account the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'account';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
        // in order to save password to database, this required rule needs to be changed
        // array('surname, title, business_id, username, password, email, tel, first_name, role_id', 'required'),
		return array(
			array('username, password', 'required'),
            // make sure username and email are unique
            array('username', 'unique', 'className'=> 'Account'),
            array('email', 'unique', 'className'=> 'Account'),

			array('business_id, role_id', 'numerical', 'integerOnly'=>true),
			array('surname, title, username, email, first_name', 'length', 'max'=>30),
			array('password', 'length', 'max'=>100),
			array('tel', 'length', 'max'=>15),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('surname, title, business_id, account_id, username, password, email, tel, first_name, role_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'business' => array(self::BELONGS_TO, 'Business', 'business_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'surname' => 'Surname',
			'title' => 'Title',
			'business_id' => 'Business',
			'account_id' => 'Account',
			'username' => 'Username',
			'password' => 'Password',
			'email' => 'Email',
			'tel' => 'Tel',
			'first_name' => 'First Name',
			'role_id' => 'Role',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('surname',$this->surname,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('business_id',$this->business_id);
		$criteria->compare('account_id',$this->account_id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('tel',$this->tel,true);
		$criteria->compare('first_name',$this->first_name,true);
		$criteria->compare('role_id',$this->role_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}